/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;

import POJOS.Album;
import POJOS.Artist;
import POJOS.MusicGroup;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.swing.JOptionPane;
import javax.swing.ListSelectionModel;

/**
 *
 * @author jim
 */

public class ListAlbumGroupForm extends javax.swing.JFrame {
    public static int selectedRow;
    Album album;
    MusicGroup musicGroup;
    
    /**
     * Creates new form ListAlbumGroupForm
     */
    public ListAlbumGroupForm() {
        initComponents();
        checkButtons();
        //Δικαίωμα επιλογής μίας γραμμής στον πίνακα
        albumGroupTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    }
    
    private void checkButtons(){
        selectedRow= albumGroupTable.getSelectedRow();
        modifyGroup.setEnabled(selectedRow>=0);
        
        if (selectedRow>=0){
            album= albumList.get(albumGroupTable.convertRowIndexToModel(selectedRow));
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        RadioStationPUEntityManager0 = java.beans.Beans.isDesignTime() ? null : javax.persistence.Persistence.createEntityManagerFactory("RadioStationPU").createEntityManager();
        albumQuery = java.beans.Beans.isDesignTime() ? null : RadioStationPUEntityManager0.createQuery("SELECT al FROM Album al WHERE al.idMusicGroup IS NOT NULL");
        albumList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : org.jdesktop.observablecollections.ObservableCollections.observableList(albumQuery.getResultList());
        jLabel1 = new javax.swing.JLabel();
        modifyGroup = new javax.swing.JButton();
        addOrDelete = new javax.swing.JButton();
        exit = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        albumGroupTable = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("ΑΡΧΕΙΟ ΑΛΜΠΟΥΜ ΣΥΓΚΡΟΤΗΜΑΤΩΝ");
        addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                formMouseClicked(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel1.setForeground(new java.awt.Color(204, 0, 204));
        jLabel1.setText("ΠΙΝAΚΑΣ ΑΛΜΠΟΥΜ ΣΥΓΚΡΟΤΗΜΑΤΩΝ");

        modifyGroup.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        modifyGroup.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/hp-customization-control-panel-icon.png"))); // NOI18N
        modifyGroup.setText("ΤΡΟΠΟΠΟΙΗΣΗ");
        modifyGroup.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                modifyGroupActionPerformed(evt);
            }
        });

        addOrDelete.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        addOrDelete.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/add-remove-user-icons-20638746.jpg"))); // NOI18N
        addOrDelete.setText("ΠΡΟΣΘΗΚΗ- ΔΙΑΓΡΑΦΗ");
        addOrDelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addOrDeleteActionPerformed(evt);
            }
        });

        exit.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        exit.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/Button-exit-icon.jpg"))); // NOI18N
        exit.setText("ΕΞΟΔΟΣ");
        exit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                exitActionPerformed(evt);
            }
        });

        org.jdesktop.swingbinding.JTableBinding jTableBinding = org.jdesktop.swingbinding.SwingBindings.createJTableBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, albumList, albumGroupTable);
        org.jdesktop.swingbinding.JTableBinding.ColumnBinding columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${title}"));
        columnBinding.setColumnName("ΤΙΤΛΟΣ");
        columnBinding.setColumnClass(String.class);
        columnBinding.setEditable(false);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${typeAlbum}"));
        columnBinding.setColumnName("ΤΥΠΟΣ ΑΛΜΠΟΥΜ");
        columnBinding.setColumnClass(String.class);
        columnBinding.setEditable(false);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${idMusicProductionCompany.nameMpc}"));
        columnBinding.setColumnName("ΕΤΑΙΡΙΑ ΠΑΡΑΓΩΓΗΣ");
        columnBinding.setColumnClass(String.class);
        columnBinding.setEditable(false);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${idMusicGroup.nameMusicGroup}"));
        columnBinding.setColumnName("ΣΥΓΚΡΟΤΗΜΑ");
        columnBinding.setColumnClass(String.class);
        columnBinding.setEditable(false);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${releaseDate}"));
        columnBinding.setColumnName("ΗΜΕΡΟΜΗΝΙΑ ΚΥΚΛΟΦΟΡΙΑΣ");
        columnBinding.setColumnClass(java.util.Date.class);
        columnBinding.setEditable(false);
        bindingGroup.addBinding(jTableBinding);
        jTableBinding.bind();
        albumGroupTable.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                albumGroupTableMouseClicked(evt);
            }
        });
        jScrollPane2.setViewportView(albumGroupTable);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(45, 45, 45)
                .addComponent(addOrDelete)
                .addGap(96, 96, 96)
                .addComponent(modifyGroup)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 143, Short.MAX_VALUE)
                .addComponent(exit)
                .addGap(33, 33, 33))
            .addGroup(layout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addComponent(jScrollPane2)
                .addContainerGap())
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jLabel1)
                .addGap(252, 252, 252))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(26, 26, 26)
                .addComponent(jLabel1)
                .addGap(28, 28, 28)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 126, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(31, 31, 31)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(addOrDelete, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(modifyGroup)
                    .addComponent(exit))
                .addContainerGap(31, Short.MAX_VALUE))
        );

        bindingGroup.bind();

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void modifyGroupActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_modifyGroupActionPerformed
        // TODO add your handling code here:
        selectedRow= albumGroupTable.getSelectedRow();
        album= albumList.get(albumGroupTable.convertRowIndexToModel(selectedRow));
        new NewOrModifyAlbumGroupForm(album).setVisible(true);
    }//GEN-LAST:event_modifyGroupActionPerformed

    private void addOrDeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addOrDeleteActionPerformed
        // TODO add your handling code here:
        //Αν δεν υπάρχει επιλεγμένη γραμμή στον πίνακα των άλμπουμ καλεί το GUI για τη δημιουργία νέου άλμπουμ
        if (selectedRow<0 ){
            new NewOrModifyAlbumGroupForm().setVisible(true);
            //Αν υπάρχει επιλεγμένη γραμμή στον πίνακα διαγράφει το επιλεγμένο άλμπουμ
        }else{
            selectedRow= albumGroupTable.getSelectedRow();
            album= albumList.get(albumGroupTable.convertRowIndexToModel(selectedRow));
        
            int choice = JOptionPane.showConfirmDialog(null,"Επιθυμείτε να διαγράψετε το album " + album.getTitle() + ""
                +  " ;","",JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE);
        
            if (choice==0){
                //Αν διαγραφεί το άλμπουμ από τη βάση,εφμανίζει επιτυχή διαγραφή του άλμπουμ
                if (DB.DBmanager.DeleteAlbum(album)){
                    //Αφαίρεση άλμπουμ από το συγκρότημα
                    if(DB.DBmanager.DeleteAlbumMusicGroup(album,album.getIdMusicGroup())){
                        
                        albumList.remove(album);
                        JOptionPane.showMessageDialog(null,"Επιτυχής διαγραφή album!","SUCCESSFULL",JOptionPane.INFORMATION_MESSAGE);
                    }else{
                        JOptionPane.showMessageDialog(null,"Αποτυχία διαγραφής album!","ERROR",JOptionPane.ERROR_MESSAGE);
                    }
                    checkButtons();    
                }         
            }
        }
    }//GEN-LAST:event_addOrDeleteActionPerformed

    private void exitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exitActionPerformed
        // TODO add your handling code here:
        //Επιβεβαίωση για έξοδο από το GUI
        int choice = JOptionPane.showConfirmDialog(null,"Επιθυμείτε έξοδο από τον πίνακα άλμπουμ συγκροτημάτων;","",JOptionPane.OK_CANCEL_OPTION,JOptionPane.QUESTION_MESSAGE); 
        
        if (choice==0){
            dispose();
        }else
    checkButtons();
    }//GEN-LAST:event_exitActionPerformed

    private void albumGroupTableMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_albumGroupTableMouseClicked
        // TODO add your handling code here:
        checkButtons();
    }//GEN-LAST:event_albumGroupTableMouseClicked

    private void formMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_formMouseClicked
        // TODO add your handling code here:
        //Με κλικ στο GUI εκτός πίνακα απενεργοποιείται το κουμπί για τροποποίηση άλμπουμ και ενεργοποιείται η προσθήκη νέου άλμουμ
        selectedRow=-1;
        modifyGroup.setEnabled(selectedRow>=0);
    }//GEN-LAST:event_formMouseClicked

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(ListAlbumGroupForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(ListAlbumGroupForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(ListAlbumGroupForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(ListAlbumGroupForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>
        //</editor-fold>
        //</editor-fold>
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new ListAlbumGroupForm().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    public static javax.persistence.EntityManager RadioStationPUEntityManager0;
    private javax.swing.JButton addOrDelete;
    public static javax.swing.JTable albumGroupTable;
    public static java.util.List<POJOS.Album> albumList;
    public static javax.persistence.Query albumQuery;
    private javax.swing.JButton exit;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JButton modifyGroup;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables
}
